package com.lianniu.mgr.baseService.staff.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.lianniu.mgr.baseService.post.model.Post2OrgVo;
import com.lianniu.mgr.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.baseService.dict.model.DictionaryKeyValueVo;
import com.lianniu.mgr.baseService.dict.model.DictionaryVo;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.baseService.staff.model.ParamVo;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import com.lianniu.mgr.baseService.staff.service.IStaffService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;

@Controller
@RequestMapping("/rest")
public class StaffRestController {
  private final static Logger logger = Logger.getLogger(StaffRestController.class);

  @Resource
  private IStaffService staffService;
  @Resource
  private IAdminAreaService adminAreaService;
  @Resource
  private IDictionaryService dictionaryService;

  /**
   * @Description:添加员工信息
   * @param request
   * @param staffVo
   * @return BaseResult

   * @Date:2016年3月31日下午2:20:26
   * @Version 1.0.0
   */
  @RequestMapping(value = "/addStaff", method = {RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult addStaff(HttpServletRequest request, StaffVo staffVo) {
    logger.info("StaffRestController addStaff begin.");
    logger.info("StaffVo:" + JSON.toJSONString(staffVo));
    try {
      logger.info("addStaff begin.");
      BaseResult baseResult = staffService.addStaff(staffVo);
      logger.info("addStaff end.");

      logger.info("StaffRestController addStaff end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("addStaff error:" + e.getMessage());
      logger.info("StaffRestController addStaff end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:编辑员工信息
   * @param request
   * @param staffVo
   * @return BaseResult

   * @Date:2016年3月31日下午2:20:29
   * @Version 1.0.0
   */
  @RequestMapping(value = "/editStaff", method = RequestMethod.POST)
  public @ResponseBody BaseResult editStaff(HttpServletRequest request, StaffVo staffVo) {
    logger.info("StaffRestController editStaff begin.");
    logger.info("StaffVo:" + JSON.toJSONString(staffVo));
    try {
      logger.info("editStaff begin.");
      BaseResult baseResult = staffService.editStaff(staffVo);
      logger.info("editStaff end.");

      logger.info("StaffRestController editStaff end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("addStaff error:" + e.getMessage());
      logger.info("StaffRestController editStaff end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description：员工列表
   * @Param:输入参数的说明
   * @Return:返回值

   * @Date:2016-3-28 28:15:11
   */
  @RequestMapping(value = {"/staffList"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult staffList(HttpServletRequest request, StaffVo staffVo,
      PaginationInfo paginationInf) {
    logger.info("StaffRestController staffList begin.");
    try {
      logger.info("getStaffList begin.");
      BaseResult baseResult = staffService.getStaffList(staffVo, paginationInf);
      logger.info("getStaffList end.");
      logger.info("StaffRestController staffList end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController staffList error:" + e.getMessage());
      logger.error("StaffRestController staffList end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description：删除员工
   * @Param:输入参数的说明
   * @Return:返回值

   * @Date:2016-3-29 21:15:11
   */
  @RequestMapping(value = {"/deleteStaff"}, method = RequestMethod.POST)
  public @ResponseBody BaseResult deleteStaff(HttpServletRequest request) {
    logger.info("StaffRestController deleteStaff begin.");
    try {
      logger.info("deleteStaff begin.");
      String modifyUser = request.getParameter("modifyUser");
      BaseResult baseResult =
          staffService.delStaff(modifyUser, request.getParameter("ids").split(","));
      logger.info("deleteStaff end.");
      logger.info("StaffRestController deleteStaff end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController deleteStaff error:" + e.getMessage());
      logger.error("StaffRestController deleteStaff end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  @RequestMapping(value = {"/getStaff"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult getStaffInfo(HttpServletRequest request, String userid) {
    logger.info("StaffRestController getStaffInfo begin.");
    logger.info("StaffRestController getStaffInfo userid:" + userid);
    StaffVo staffVo = new StaffVo();
    staffVo.setStaffId(userid);
    try {
      BaseResult baseResult = staffService.getStaff(staffVo);
      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("StaffRestController getStaffInfo end.");
        return baseResult;
      }
      StaffVo retStaffVo = (StaffVo) baseResult.getData();
      // dictionaryService.selectByDictName(curDictVo, paginationInfo);

      DictionaryVo dictionaryVo = new DictionaryVo();
      dictionaryVo.setTypeName("性别");
      BaseResult retDict = dictionaryService.selectDictionaryByDicType(dictionaryVo);
      List<DictionaryKeyValueVo> data = (List<DictionaryKeyValueVo>) retDict.getData();
      List<DictionaryKeyValueVo> RetDictList = data;
      if (RetDictList != null && RetDictList.size() > 0) {
        retStaffVo.setSex(setDictText(RetDictList, retStaffVo.getSex(), retStaffVo));
      }
      dictionaryVo.setTypeName("职位状态");
      BaseResult retDict2 = dictionaryService.selectDictionaryByDicType(dictionaryVo);
      List<DictionaryKeyValueVo> RetDictList2 = (List<DictionaryKeyValueVo>) retDict2.getData();
      if (RetDictList2 != null && RetDictList2.size() > 0) {
        retStaffVo
                .setEmployStatus(setDictText(RetDictList2, retStaffVo.getEmployStatus(), retStaffVo));
      }


      logger.info("StaffRestController getStaffInfo end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController getStaffInfo error:" + e.getMessage());
      logger.error("StaffRestController getStaffInfo end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  private String setDictText(List<DictionaryKeyValueVo> RetDictList,String DictValue,StaffVo retStaffVo){
    String dict="";
    for(DictionaryKeyValueVo dictionaryKeyValueVo:RetDictList){
      if(dictionaryKeyValueVo.getDicValue().equals(DictValue)){
        dict=dictionaryKeyValueVo.getDicName();
      }
    }
    return dict;
  }

  /**
   * @Description:编辑时显示的员工信息
   * @param request
   * @return BaseResult

   * @Date:2016年4月6日下午12:56:50
   * @Version 1.0.0
   */
  @RequestMapping(value="/editInfo",method=RequestMethod.POST)
  public @ResponseBody BaseResult editInfo(HttpServletRequest request){
	 logger.info("StaffRestController editInfo begin.");
	 logger.info("staffId:"+request.getParameter("staffId"));
	 String staffId=request.getParameter("staffId");
	 StaffVo staffVo=new StaffVo();
	 staffVo.setStaffId(staffId);
	 try{
		 logger.info("getStaff begin.");
		 BaseResult baseResult=staffService.getStaff(staffVo);
		 logger.info("getStaff end.");
		 logger.info("StaffRestController editInfo end.");
		 return baseResult;
	 }catch(Exception e){
		 logger.error("getStaff error:"+e.getMessage());
		 logger.info("StaffRestController editInfo end.");
		 return BaseResult.exception(e.getMessage());
	 }
  }

  /**
   * @Description:分配岗位
   * @param post2OrgVo
   * @param paginationInfo
   * @return BaseResult
   * @Date:2016年10月21日上午11:51:35
   * @Version 1.0.0
   */
  @RequestMapping(value = "/assignPost", method = RequestMethod.GET)
  public @ResponseBody BaseResult assignPost(Post2OrgVo post2OrgVo, PaginationInfo paginationInfo) {
    logger.info("StaffRestController assignPost begin.");
    logger.info("员工组织岗位信息&post2OrgVo:" + JSON.toJSONString(post2OrgVo));
    logger.info("分页信息&paginationInfo:" + JSON.toJSONString(paginationInfo));

    logger.info("StaffRestController assignPost input data check begin.");
    if (StringUtil.isEmpty(post2OrgVo.getStaffId())) {
      logger.error("入参staffId为空");
      logger.info("StaffRestController assignPost input data check end.");
      logger.info("StaffRestController assignPost end.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("StaffRestController assignPost input data check end.");

    try {
      logger.info("StaffRestController assignPost assignPost begin.");
      BaseResult baseResult = staffService.assignPost(post2OrgVo, paginationInfo);
      logger.info("StaffRestController assignPost assignPost end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController assignPost assignPost error:", e);
      logger.info("StaffRestController assignPost assignPost end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:保存岗位
   * @param request
   * @return BaseResult
   * @Date:2016年4月6日下午12:56:34
   * @Version 1.0.0
   */
  @RequestMapping(value = "/savePost", method = RequestMethod.POST)
  public @ResponseBody BaseResult savePost(ParamVo paramVo) {
    logger.info("StaffRestController savePost begin.");
    logger.info("paramVo:" + JSON.toJSONString(paramVo));

    logger.info("StaffRestController savePost input data check begin.");
    if (StringUtil.isEmpty(paramVo.getStaffId())) {
      logger.error("入参staffId为空");
      logger.info("StaffRestController savePost input data check end.");
      logger.info("StaffRestController savePost end.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("StaffRestController savePost input data check end.");

    if (paramVo.getAdds() == null && paramVo.getDels() == null) {
      logger.info("岗位分配未有改动");
      return BaseResult.success();
    }

    try {
      logger.info("StaffRestController savePost addPost begin.");
      BaseResult baseResult = staffService.addPost(paramVo);
      logger.info("StaffRestController savePost addPost end.");
      logger.info("StaffRestController savePost end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController savePost addPost error:", e);
      logger.info("StaffRestController savePost end.");
      return BaseResult.exception(e.getMessage());
    }


  }

  /**
   * @Description:检证员工编号是否存在
   * @param request
   * @return BaseResult

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistStaffNo",method=RequestMethod.GET)
  public @ResponseBody boolean isExistStaffNo(HttpServletRequest request){
    logger.info("StaffRestController isExistStaffNo begin.");

    //员工编号
    String staffNo=request.getParameter("staffNo");
    logger.info("staffNo=" + staffNo);

    try{
      logger.info("isExistStaffNo begin.");
      boolean exitFlg=staffService.isExistStaffNo(staffNo);
      logger.info("isExistStaffNo end.");
      logger.info("StaffRestController isExistStaffNo end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistStaffNo error:"+e.getMessage());
      logger.info("StaffRestController isExistStaffNo end.");
      return false;
    }
  }

  /**
   * @Description:检证员工用户名是否存在
   * @param request
   * @return BaseResult

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistLoginName",method=RequestMethod.GET)
  public @ResponseBody boolean isExistLoginName(HttpServletRequest request){
    logger.info("StaffRestController isExistLoginName begin.");

    //登录名
    String loginName=request.getParameter("loginName");
    logger.info("loginName=" + loginName);

    try{
      logger.info("isExistLoginName begin.");
      boolean exitFlg=staffService.isExistLoginName(loginName);
      logger.info("isExistLoginName end.");
      logger.info("StaffRestController isExistLoginName end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistLoginName error:"+e.getMessage());
      logger.info("StaffRestController isExistLoginName end.");
      return false;
    }
  }

  /**
   * @Description:检证员工手机号是否存在
   * @param request
   * @return BaseResult

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistMobile",method=RequestMethod.GET)
  public @ResponseBody boolean isExistMobile(HttpServletRequest request){
    logger.info("StaffRestController isExistMobile begin.");

    //手机号
    String mobile=request.getParameter("mobile");
    logger.info("mobile=" + mobile);

    try{
      logger.info("isExistMobile begin.");
      boolean exitFlg=staffService.isExistMobile(mobile);
      logger.info("isExistMobile end.");
      logger.info("StaffRestController isExistMobile end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistMobile error:"+e.getMessage());
      logger.info("StaffRestController isExistMobile end.");
      return false;
    }
  }

  /**
   * @Description:检证员工邮箱是否存在
   * @param request
   * @return BaseResult

   * @Date:2016年4月7日下午12:56:34
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistEmail",method=RequestMethod.GET)
  public @ResponseBody boolean isExistEmail(HttpServletRequest request){
    logger.info("StaffRestController isExistEmail begin.");

    //邮箱地址
    String email=request.getParameter("email");
    logger.info("email=" + email);

    try{
      logger.info("isExistEmail begin.");
      boolean exitFlg=staffService.isExistEmail(email);
      logger.info("isExistEmail end.");
      logger.info("StaffRestController isExistEmail end.");
      return exitFlg;

    }catch(Exception e){
      logger.info("isExistMobile error:"+e.getMessage());
      logger.info("StaffRestController isExistEmail end.");
      return false;
    }
  }

  /**
   * @Description:检证员工编号是否存在
   * @param boolean
   * @return BaseResult

   * @Date:2016-4-7 21:35:08
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistStaffNoById",method=RequestMethod.GET)
  public @ResponseBody boolean isExistStaffNoById(HttpServletRequest request){
    logger.info("StaffRestController isExistStaffNoById begin.");

    //员工编号
    String staffNo=request.getParameter("staffNo");
    //员工ID
    String staffId=request.getParameter("staffId");
    logger.info("staffNo=" + staffNo);
    logger.info("staffId=" + staffId);

    try{
      logger.info("isExistStaffNoById begin.");
      boolean exitFlg=staffService.isExistStaffNoById(staffNo,staffId);
      logger.info("isExistStaffNoById end.");
      logger.info("StaffRestController isExistStaffNoById end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistStaffNo error:"+e.getMessage());
      logger.info("StaffRestController isExistStaffNoById end.");
      return false;
    }
  }

  /**
   * @Description:检证员工用户名是否存在
   * @param boolean
   * @return BaseResult

   * @Date:2016-4-7 21:35:08
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistLoginNameById",method=RequestMethod.GET)
  public @ResponseBody boolean isExistLoginNameById(HttpServletRequest request){
    logger.info("StaffRestController isExistLoginNameById begin.");

    //登录名
    String loginName=request.getParameter("loginName");
    //员工ID
    String staffId=request.getParameter("staffId");
    logger.info("loginName=" + loginName);
    logger.info("staffId=" + staffId);

    try{
      logger.info("isExistLoginNameById begin.");
      boolean exitFlg=staffService.isExistLoginNameById(loginName,staffId);
      logger.info("isExistLoginNameById end.");
      logger.info("StaffRestController isExistLoginNameById end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistLoginName error:"+e.getMessage());
      logger.info("StaffRestController isExistLoginNameById end.");
      return false;
    }
  }

  /**
   * @Description:检证员工手机号是否存在
   * @param boolean
   * @return BaseResult

   * @Date:2016-4-7 21:35:08
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistMobileById",method=RequestMethod.GET)
  public @ResponseBody boolean isExistMobileById(HttpServletRequest request){
    logger.info("StaffRestController isExistMobileById begin.");

    //手机号
    String mobile=request.getParameter("mobile");
    //员工ID
    String staffId=request.getParameter("staffId");
    logger.info("mobile=" + mobile);
    logger.info("staffId=" + staffId);

    try{
      logger.info("isExistMobileById begin.");
      boolean exitFlg=staffService.isExistMobileById(mobile,staffId);
      logger.info("isExistMobileById end.");
      logger.info("StaffRestController isExistMobileById end.");
      return exitFlg;
    }catch(Exception e){
      logger.info("isExistMobile error:"+e.getMessage());
      logger.info("StaffRestController isExistMobileById end.");
      return false;
    }
  }

  /**
   * @Description:检证员工邮箱是否存在
   * @param boolean
   * @return BaseResult

   * @Date:2016-4-7 21:35:08
   * @Version 1.0.0
   */
  @RequestMapping(value="/isExistEmailById",method=RequestMethod.GET)
  public @ResponseBody boolean isExistEmailById(HttpServletRequest request){
    logger.info("StaffRestController isExistEmailById begin.");

    //邮箱地址
    String email=request.getParameter("email");
    //员工ID
    String staffId=request.getParameter("staffId");
    logger.info("email=" + email);
    logger.info("staffId=" + staffId);

    try{
      logger.info("isExiisExistEmailByIdstEmail begin.");
      boolean exitFlg=staffService.isExistEmailById(email,staffId);
      logger.info("isExistEmailById end.");
      logger.info("StaffRestController isExistEmailById end.");
      return exitFlg;

    }catch(Exception e){
      logger.info("isExistMobile error:"+e.getMessage());
      logger.info("StaffRestController isExistEmailById end.");
      return false;
    }
  }


  /**
   * @Description:页面删除岗位，员工是否还存在岗位(1有，0没有)
   * @param staffId
   * @return

   * @Date:2016年11月23日上午10:08:23
   * @Version 1.0.0
   */
  @RequestMapping("isPostExistByStaffId")
  public @ResponseBody BaseResult isPostExistByStaffId(String staffId, String[] dels) {
    logger.info("StaffRestController isPostExistByStaffId begin.");
    logger.info("员工id:" + staffId);

    logger.info("StaffRestController isPostExistByStaffId input data check begin.");
    if (StringUtil.isEmpty(staffId)) {
      logger.error("入参staffId为空");
      logger.info("StaffRestController isPostExistByStaffId input data check end.");
      logger.info("StaffRestController isPostExistByStaffId begin.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("StaffRestController isPostExistByStaffId input data check end.");

    try {
      logger.info("StaffRestController isPostExistByStaffId isPostExistByStaffId begin.");
      BaseResult baseResult = staffService.isPostExistByStaffId(staffId, dels);
      logger.info("StaffRestController isPostExistByStaffId isPostExistByStaffId end.");
      logger.info("StaffRestController isPostExistByStaffId end.");
      return baseResult;
    } catch (Exception e) {
      logger.info("StaffRestController isPostExistByStaffId isPostExistByStaffId error:", e);
      logger.info("StaffRestController isPostExistByStaffId end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  /**
   * @Description:重置密码，默认设置为1q2w3e
   * @param staffId
   * @Date:2016年12月12日
   */
  @RequestMapping("resetPwd")
  public @ResponseBody BaseResult resetPwd(String staffId,String username){
    logger.info("StaffRestController resetPwd begin.");
    logger.info("StaffRestController resetPwd staffId&员工id:"+staffId);
    logger.info("StaffRestController resetPwd username&修改者:"+username);

    logger.info("StaffRestController resetPwd input data check begin.");
    if(StringUtil.isEmpty(staffId) || StringUtil.isEmpty(username)){
      logger.error("StaffRestController resetPwd 入参为空");
      logger.info("StaffRestController resetPwd input data check end.");
      logger.info("StaffRestController resetPwd begin.");
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("StaffRestController resetPwd input data check end.");

    try{
      logger.info("StaffRestController resetPwd resetPwdByStaffId begin.");
      BaseResult baseResult=staffService.resetPwdByStaffId(staffId,username);
      logger.info("StaffRestController resetPwd resetPwdByStaffId end.");
      logger.info("StaffRestController resetPwd end.");
      return baseResult;
    }catch(Exception e){
      logger.info("StaffRestController resetPwd resetPwdByStaffId error:",e);
      logger.info("StaffRestController resetPwd end.");
      return BaseResult.exception(e.getMessage());
    }
  }
  /**
   * @Description:查看用户数据权限
   * @param request
   * @param staffId
   * @return BaseResult
   * @Date:2016年10月15日下午12:39:17
   * @Version 1.0.0
   */
  @RequestMapping(value = "/checkPrecinct", method = RequestMethod.GET)
  public @ResponseBody BaseResult precinctAuthorityList(HttpServletRequest request,
                                                        String staffId) {
    logger.info("StaffRestController precinctAuthorityList begin.");
    logger.info("员工id&staffId:" + staffId);

    try {
      logger.info("StaffRestController precinctAuthorityList checkPrecintByStaffId begin.");
      BaseResult baseResult = staffService.checkPrecintByStaffId(staffId);
      logger.info("StaffRestController precinctAuthorityList checkPrecintByStaffId end.");
      logger.info("StaffRestController precinctAuthorityList end.");
      return baseResult;
    } catch (Exception e) {
      logger.info("StaffRestController precinctAuthorityList checkPrecintByStaffId error:", e);
      logger.info("StaffRestController precinctAuthorityList end.");
      return BaseResult.exception(e.getMessage());
    }
  }

  //根据岗位获取员工
  @RequestMapping(value="/selectStaffByPostName",method={RequestMethod.GET,RequestMethod.POST})
  public @ResponseBody BaseResult selectStaffByPostName(HttpServletRequest request,String postName) {
    logger.info("StaffRestController selectStaffByPostName begin.");
    try {
      BaseResult baseResult = staffService.selectStaffByPostName(postName);
      logger.info("StaffRestController end.");
      logger.info("StaffRestController selectStaffByPostName end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("StaffRestController selectStaffByPostName error:" + e.getMessage());
      logger.error("StaffRestController selectStaffByPostName end.");
      return BaseResult.exception(e.getMessage());
    }
  }

}
